package pro.cherish.knowledgeutil.column;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 创建人：cherish
 * @version 版本号：V1.0
 * <p>
 * ***************************修订记录************************************
 * 2019-11-01 cherish 创建该类功能。
 * **********************************************************************
 * </p>
 * @ClassName 类名：ColumnUtilExt
 * @Description 功能说明：
 * <p>
 * TODO
 * </p>
 * **********************************************************************
 * @date 创建日期： 2019-11-01 cherish
 */
public class ColumnUtilExt {
    /**
     * 下划线转小驼峰
     *
     * @param column 原字符串
     * @return
     */
    public static String underlineToCamel(String column) {
        StringBuilder sb = new StringBuilder();
        boolean nextUpperCase = false;
        for (int i = 0; i < column.length(); i++) {
            char c = column.charAt(i);
            if (c == '_') {
                if (sb.length() > 0) {
                    nextUpperCase = true;
                }
            } else {
                if (nextUpperCase) {
                    sb.append(Character.toUpperCase(c));
                    nextUpperCase = false;
                } else {
                    sb.append(Character.toLowerCase(c));
                }
            }
        }
        return sb.toString();
    }

    // 匹配大写字母接着小写字母
    private static Pattern Aa_PATTERN = Pattern.compile("[A-Z]([a-z\\d]+)?");

    /**
     * 驼峰转为下滑线
     *
     * @param line 字符串
     * @return
     */
    public static String camelUnderline(String line) {
        if (line == null || "".equals(line)) {
            return "";
        }
        line = String.valueOf(line.charAt(0)).toUpperCase().concat(line.substring(1));
        StringBuffer sb = new StringBuffer();
        Matcher matcher = Aa_PATTERN.matcher(line);
        while (matcher.find()) {
            String word = matcher.group();
            sb.append(word.toUpperCase());
            sb.append(matcher.end() == line.length() ? "" : "_");
        }
        return sb.toString();
    }

}
